﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity.Validation;
using System.Data.Entity.Infrastructure;
using XStudio.XCRF.Data.Entities;
using XStudio.XCRF.Data.Enums;

namespace XStudio.XCRF.Data.Business.Validate
{
    /// <summary>
    /// 病例报告验证属性
    /// </summary>
    public class CRFValidate : ValidateAttribute
    {
        public override IEnumerable<DbValidationError> ValidateAdd(DbEntityEntry entityEntry, XCRFContext context)
        {
            List<DbValidationError> errors = new List<DbValidationError>();
            CRF crf = (CRF)entityEntry.Entity;
            Member currentMember = context.CurrentMember;
            switch ((MemberTypes)currentMember.Type)
            {
                case MemberTypes.Administrator: break;
                case MemberTypes.Owner:
                    if (crf.HospitalID != currentMember.HospitalID)
                        errors.Add(new DbValidationError("HospitalID", "只能为自己的医院建立病例报告"));
                    break;
                case MemberTypes.Researcher:
                    if (crf.HospitalID != currentMember.HospitalID)
                        errors.Add(new DbValidationError("HospitalID", "只能为自己的医院建立病例报告"));
                    if (crf.ResearcherID != currentMember.ID)
                        errors.Add(new DbValidationError("ResearcherID", "研究者只能为自己添加病例报告"));
                    break;
            }
            return errors;
        }

        public override IEnumerable<DbValidationError> ValidateEdit(DbEntityEntry entityEntry, XCRFContext context)
        {
            List<DbValidationError> errors = new List<DbValidationError>();
            CRF crf = (CRF)entityEntry.Entity;
            Member currentMember = context.CurrentMember;
            switch ((MemberTypes)currentMember.Type)
            {
                case MemberTypes.Administrator: break;
                case MemberTypes.Owner:
                    if (crf.HospitalID != currentMember.HospitalID)
                        errors.Add(new DbValidationError("HospitalID", "只能编辑自己的医院的病例报告"));
                    break;
                case MemberTypes.Researcher:
                    if (crf.HospitalID != currentMember.HospitalID)
                        errors.Add(new DbValidationError("HospitalID", "只能编辑自己的医院的病例报告"));
                    if (crf.ResearcherID != currentMember.ID)
                        errors.Add(new DbValidationError("ResearcherID", "研究者只能编辑自己添加的病例报告"));
                    break;
            }
            return errors;
        }

        public override IEnumerable<DbValidationError> ValidateDelete(DbEntityEntry entityEntry, XCRFContext context)
        {
            List<DbValidationError> errors = new List<DbValidationError>();
            CRF crf = (CRF)entityEntry.Entity;
            Member currentMember = context.CurrentMember;
            switch ((MemberTypes)currentMember.Type)
            {
                case MemberTypes.Administrator: break;
                case MemberTypes.Owner:
                    if (crf.HospitalID != currentMember.HospitalID)
                        errors.Add(new DbValidationError("HospitalID", "只能删除自己的医院的病例报告"));
                    break;
                case MemberTypes.Researcher:
                    if (crf.HospitalID != currentMember.HospitalID)
                        errors.Add(new DbValidationError("HospitalID", "只能删除自己的医院的病例报告"));
                    if (crf.ResearcherID != currentMember.ID)
                        errors.Add(new DbValidationError("ResearcherID", "研究者只能删除自己添加的病例报告"));
                    break;
            }
            return errors;
        }
    }
}
